package com.ray.controller;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.MaterialTreeBean;
import com.ray.exception.ServiceException;
import com.ray.service.EnergyService;
import com.ray.service.StructureTreeService;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import com.ray.util.ReturnStringUtil;

@Controller
@RequestMapping("/structureTree")
public class StructureTreeController {
	@Autowired
	StructureTreeService structureTreeService;
	
	@RequestMapping("/{methodName}")
	@ResponseBody
	private Map<String, Object> addAirFlowControl(HttpServletRequest request,HttpServletResponse response,
			@PathVariable("methodName") String methodName) throws NoSuchMethodException, SecurityException, IllegalAccessException {
		Map<String, Object> requestParamMap = RequestUtil.getRequestAllParam(request);
//		requestParamMap.put("request", request);
//		requestParamMap.put("response", response);
		Method targetMethod = structureTreeService.getClass().getMethod(urlMethodMappingMap.get(methodName), Map.class);
		Object object=null;
		try {
			 object=targetMethod.invoke(structureTreeService, requestParamMap);
		}catch(InvocationTargetException e) {
			if(e.getCause().getClass()== IllegalArgumentException.class || e.getCause().getClass() ==ServiceException.class) {
				e.printStackTrace();
				Assert.notNull(null,e.getTargetException().getMessage());
			}else {
				e.printStackTrace();
				return ResponseUtil.ResponseError("网络不好，请稍等。。。。");
			}
		}
		return ResponseUtil.ResponseResult(object);
	}

	/**
	 *  方法名和请求路径映射
	 */
	private static  Map<String, String> urlMethodMappingMap;
	static{
		urlMethodMappingMap=new HashMap<String,String>();
		//结构树
		urlMethodMappingMap.put("getMaterialTreeData", "getMaterialTreeData");//获取物料配置结构树
		urlMethodMappingMap.put("addMaterialTreeData", "addMaterialTreeData");//添加物料配置结构树
		urlMethodMappingMap.put("updateMaterialTreeData", "updateMaterialTreeData");//修改物料配置结构树
		urlMethodMappingMap.put("deleteMaterialTreeData", "deleteMaterialTreeData");//删除物料配置结构树
		urlMethodMappingMap.put("getMaterialTreeByMap", "getMaterialTreeByMap");//查询物料配置结构树
		urlMethodMappingMap.put("geAlltMaterialTree", "geAlltMaterialTree");//查询物料配置结构树
		
		//半成品设置
		urlMethodMappingMap.put("getCompanyHalfMaterialName", "getCompanyHalfMaterialName");//获取公司半成品组合名称
		urlMethodMappingMap.put("addCompanyHalfMaterial", "addCompanyHalfMaterial");//添加半成品配置
		urlMethodMappingMap.put("updateHalfMaterialName", "updateHalfMaterialName");//修改半成品组合名称
		urlMethodMappingMap.put("getHalfMaterialNameList", "getHalfMaterialNameList");//获取半架名称
		urlMethodMappingMap.put("addHalfMaterialType", "addHalfMaterialType");//添加半架组合物料
		urlMethodMappingMap.put("deleteHalfMaterialType", "deleteHalfMaterialType");//删除半架组合物料
		urlMethodMappingMap.put("getHalfMaterialType", "getHalfMaterialType");//获取半架物料组合
		urlMethodMappingMap.put("updateHalfMaterialType", "updateHalfMaterialType");//修改半架组合物料报工时是否显示
		urlMethodMappingMap.put("getHalfMaterialTypeList", "getHalfMaterialTypeList");//获取物料类型及半架信息
		
		
		//获取订单半架物料信息
		urlMethodMappingMap.put("getAutoOrderMaterialGroup", "getAutoOrderMaterialGroup");//获取物料自动组合的信息
		
	}
	
}
